﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.Revit;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.UI.Selection;

namespace demansion
{
    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    class newDemansion: IExternalCommand
    {
       
        Result IExternalCommand.Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;
            Level lv = doc.ActiveView.Level;
            XYZ start = new XYZ(0,0,0);
            XYZ end = new XYZ(10,10,0);
            Line li=doc.Application.Create.NewLine(start,end,true);
            FilteredElementCollector collector = new FilteredElementCollector(doc);
            ICollection<Element> collection = collector.OfClass(typeof(Grid)).ToElements();
            Element w1 = (from c in collection select c).First();
            Transaction transaction = new Transaction(doc, "Create Wall");


            transaction.Start();
            //doc.Create.NewWall(li, w1.Level, true);
            //transaction.Commit();
            List<Grid> e1, e2;       
            sortGrid(collection, out e1, out e2);

            ReferenceArray ra = new ReferenceArray();
            ReferenceArray ra2 = new ReferenceArray();
            Reference rf=null;
            int i = 0;
            foreach (Grid e in e1)
            {
                rf = e.Curve.Reference;
                ra.Insert(rf, i);
                i += 1;

            } 


            i = 0;            
            foreach (Grid e in e2)
            {
                rf = e.Curve.Reference;
                ra2.Insert(rf, i);
                i += 1;
            
            }
            try
            {
                Line li2 = doc.Application.Create.NewLine(e1.First().Curve.get_EndPoint(0), e1.Last().Curve.get_EndPoint(0), true);
                Line li3 = doc.Application.Create.NewLine(e2.First().Curve.get_EndPoint(0),e2.Last().Curve.get_EndPoint(0),true);

                doc.Create.NewDimension(doc.ActiveView, li2, ra);
                doc.Create.NewDimension(doc.ActiveView, li3, ra2);

                XYZ start1 =new XYZ(e1.First().Curve.get_EndPoint(0).X-800,e1.First().Curve.get_EndPoint(0).Y,0);
                XYZ end1 = new XYZ(e1.Last().Curve.get_EndPoint(0).X, e1.Last().Curve.get_EndPoint(0).Y-800, 0);
                Line li4 = doc.Application.Create.NewLine(start1,end1,false);

                transaction.Commit();
                TaskDialog.Show("完成","轴网新建成功11……");
            }
            catch (Exception e)
            {
                message = e.Message;
                return Result.Failed;
            
            }
            return Result.Succeeded;

        }

        public void sortGrid(ICollection<Element> es, out List<Grid> es1, out List<Grid> es2)
       {

            //将轴网分类
           es1=new List<Grid>();
           es2=new List<Grid>();
           
           string info = null;
           double a, b;
           foreach (Grid g in es)
           {

              if (g.Curve.get_EndPoint(0).X == g.Curve.get_EndPoint(1).X)
              {
                         
                  es1.Add(g);
                  a = g.Curve.get_EndPoint(0).X;
                  b = g.Curve.get_EndPoint(0).Y;

                  info += "X0:" + g.Curve.get_EndPoint(0).X + "\n\tY0:" + g.Curve.get_EndPoint(0).Y + "\n\tY0:";

              }
              else
              {
                  es2.Add(g);
              
              }
           
           }
          //TaskDialog.Show("轴网信息：",info);

       }

        public void test()
        {

            List<string> a=new List<string>();
            string x = "顶你给肺";
            a.Add(x);
            TaskDialog.Show("d",a.First().ToString());

        }
    }
}
